<?php
function formatsize ($size)
{
    $kb = 1024;
    $mb = 1048576;
    $gb = 1073741824;
    $tb = 1.09951e+012;
    if ($size < $kb)
    {
        return $size . " b";
    }
    if ($size < $mb)
    {
        $s = round($size / $kb, 0);
        return round($size / $kb, 3 - strlen($s)) . " Kb";
    }
    if ($size < $gb)
    {
        $s = round($size / $mb, 0);
        return round($size / $mb, 3 - strlen($s)) . " Mb";
    }
    if ($size < $tb)
    {
        $s = round($size / $gb, 0);
        return round($size / $gb, 3 - strlen($s)) . " Gb";
    }
    $s = round($size / $tb, 0);
    return round($size / $tb, 3 - strlen($s)) . " Tb";
}

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$gametype = intval(getvar("gametype"));
if ($curruser['prev_id'] != 0)
{
    exit();
}
$gType = chkGametype($gametype);
switch ($gType)
{
    case 0 :
        $ui = new UI();
        include( "admin_menu.php" );
        break;
    default :
        include_once( "class_3d/CheckMode_3d.php" );
        $chk = new CheckMode_3d($curruser['acl'], 1, $gametype);
        $chk->checkShow3dPage();
        unset($chk);
        $ui = new UI_3D();
        $jslink = "<script language=\"JScript.Encode\" src=\"site.js\"></script>" . ( "<script language=\"VBScript.Encode\" src=\"site.vbs\"></script>" ) . "<script src=\"core/tpl/k2/jquery-1.4.2.min.js\"></script>";
        $ui->set("jslink", $jslink);
        $ui->header();
        include( "admin_menu_3d.php" );
        break;
}
$dt = date("YmdHis", mktime(0, 0, 0, date("m"), date("d") - 30, date("Y")));
getdb("delete from k_log where logindt < {$dt}");
$i = 5;
while (1 < $i)
{
    $rs = getdb("!!select id,enabled from k_user where enabled<>1 and acl=" . $i);
    while (!$rs->eof)
    {
        $userlist = GetUserList($rs->f['id'], $i - 1);
        if ($userlist != "")
        {
            if ($rs->f['enabled'] == 0)
            {
                getdb("update k_user set amount=0,amount_3d=0,amount_3s1=0,enabled=0,updateid=" . $curruser['id'] . " ,updatedt='" . systime() . "' where id in ({$userlist}) and enabled>0");
                getdb("update amounts set avail=0,enabled=0 where user_id in ({$userlist})");
                getdb("update amounts_3d set avail=0,enabled=0 where user_id in ({$userlist})");
                getdb("update amounts_3t set avail=0,enabled=0 where user_id in ({$userlist})");
                getdb("update amounts_3s1 set avail=0,enabled=0 where user_id in ({$userlist})");
                getdb("update amounts_3s2 set avail=0,enabled=0 where user_id in ({$userlist})");
                getdb("update amounts_3s3 set avail=0,enabled=0 where user_id in ({$userlist})");
                getdb("update amounts_3s4 set avail=0,enabled=0 where user_id in ({$userlist})");
                getdb("update amounts_5h set avail=0,enabled=0 where user_id in ({$userlist})");
            }
            else
            {
                getdb("update k_user set enabled=" . $rs->f['enabled'] . ",updateid=" . $curruser['id'] . " ,updatedt='" . systime() . "' where id in ({$userlist}) and enabled>0");
                getdb("update amounts set enabled=" . $rs->f['enabled'] . " where user_id in ({$userlist})");
                getdb("update amounts_3d set enabled=" . $rs->f['enabled'] . " where user_id in ({$userlist})");
                getdb("update amounts_3t set enabled=" . $rs->f['enabled'] . " where user_id in ({$userlist})");
                getdb("update amounts_3s1 set enabled=" . $rs->f['enabled'] . " where user_id in ({$userlist})");
                getdb("update amounts_3s2 set enabled=" . $rs->f['enabled'] . " where user_id in ({$userlist})");
                getdb("update amounts_3s3 set enabled=" . $rs->f['enabled'] . " where user_id in ({$userlist})");
                getdb("update amounts_3s4 set enabled=" . $rs->f['enabled'] . " where user_id in ({$userlist})");
                getdb("update amounts_5h set enabled=" . $rs->f['enabled'] . " where user_id in ({$userlist})");
            }
        }
        $rs->movenext();
    }
    --$i;
}
sess_gc(600);
$conf = new Config();
echo "<table border=0 cellpadding=2 cellspacing=1 class=table0>";
echo "<tr class=td0><td>Table<td>Size<td>Status<td>Saved";
$rs = getdb("SHOW TABLE STATUS FROM " . G_DBDATABASE);
$total_gain = 0;
$total_all = 0;
while (!$rs->eof)
{
    $row = $rs->f;
    $tot_data = $row['Data_length'];
    $tot_idx = $row['Index_length'];
    $total = $tot_data + $tot_idx;
    $total_all += $total;
    $gain = $row['Data_free'];
    $total_gain += $gain;
    $local_query = "OPTIMIZE TABLE " . $row['Name'];
    $resultat = getdb($local_query);
    if ($gain == 0)
    {
        echo "<tr class=td2><td>{$row[0]}</td>" . "<td align=right>" . formatsize($total) . "</td>" . "<td>已经最佳化</td><td align=right></td></tr>";
    }
    else
    {
        echo "<tr class=td2><td><b>{$row[0]}</b></td>" . "<td align=right><b>" . formatsize($total) . "</b></td>" . "<td><b>最佳化</b></td><td align=right><b>" . formatsize($gain) . "</b></td></tr>";
    }
    $rs->movenext();
}
echo "<tr class=td0><td><b>Total</b></td><td align=right>" . formatsize($total_all) . "</td><td></td><td align=right>" . formatsize($total_gain) . "</td>";
echo "</table>";
?>